<template>
  <div class="login-container">
    <el-form class="login-form" ref="login-form" :rules="formRules" :model="user">
      <el-form-item>
        <img src="./logo.png" alt="" />
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input v-model="user.mobile" prefix-icon="el-icon-mobile" placeholder="请输入手机号">
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="user.code" prefix-icon="el-icon-chat-dot-round" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree"
          >我已阅读并同意用户协议和隐私条款</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-button class="login-btn" :loading='Loding' type="primary" @click="onLogin"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import request from '@/utils/request'
import { login } from '@/api/user'
export default {
  name: 'LoginIndex',

  data () {
    return {
      user: {
        mobile: '13911111111', // 手机号
        code: '246810', // 验证码
        agree: false
      },
      // checked: false, // 是否选中协议的状态
      Loding: false, // Loding，默认关闭
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3|5|7|8|9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码格式', trigger: 'blur' }
        ],
        agree: [
          {
            // 自定义检验规则
            //  验证通过：callback()
            //  验证失败：callback(new Error('错误消息'))
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请勾选用户协议'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    onLogin () {
      // 获取数据
      // const user = this.user
      // 表单手动验证
      this.$refs['login-form'].validate(valid => {
        console.log(valid)
        if (!valid) {
          return
        }
        this.login()
      })
    },
    login () {
      // 开启Loding
      this.Loding = true
      // 发送请求
      // request({
      // method: 'POST',
      // url: '/mp/v1_0/authorizations',
      // data用来设置请求体
      // data: this.user
      // })
      login('/mp/v1_0/authorizations', this.user)
        .then((res) => {
          console.log(res)
          // 关闭Loding
          this.Loding = false
          // 跳转到首页
          this.$router.push({ name: 'home' })
          // this.$router.push('/')
          window.localStorage.setItem('user', JSON.stringify(res.data.data))
          this.$message({
            message: '登录成功',
            type: 'success'
          })
        })
        .catch((err) => {
          console.log(err)
          // 关闭Loding
          this.Loding = false
          this.$message.error('登录错误，手机号或验证码错误')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("./beijing.jpg") no-repeat;
  background-size: cover;
  .login-form {
    background-color: #fff;
    padding: 50px;
    min-width: 300px;
    .login-btn {
      width: 100%;
    }
    img {
      width: 259px;
      height: 57px;
    }
  }
}
</style>
